import { defineStore } from "pinia";
import { requestLogin } from "@/api/user";
// @ts-ignore
import type { loginForm } from "@/api/user"
import { defaultRoute } from "@/router/default";

let useLoginStore = defineStore('login', {
    state: () => {
        return {
            token: localStorage.getItem('TOKEN'),
            menuRoute:defaultRoute
        }
    },

    actions: {
        async formVerify(form: loginForm) {
            let result = await requestLogin(form)
            if (result.code == 200) {
                // 类型断言,token存在时为string,不存在时为null
                this.token = (result.data.token as string)
                // 将登录成功后获取的token存储在本地
                localStorage.setItem('TOKEN',(result.data.token as string) )
            } else {
                return Promise.reject(new Error(result.data.message))
            }
        }
    }
})

export default useLoginStore